<template>
    <!-- 返回 -->
    <van-nav-bar title="申请退货退款" left-text="返回" left-arrow @click-left="back" />

    <van-form @submit="onSubmit">

        <!-- 退款类型 -->
        <div class="son">
            <van-field v-model="退货退款" label="申请类型" required name="type" model-value="仅退款" readonly />
        </div>

        <!-- 退货价格 -->
        <van-field v-model="total" label="退款金额" name="total" required  :rules="rules.total"/>
           
        <!-- 提交按钮 -->
        <div style="margin: 16px; width: 50%; margin:5rem  auto;">
            <van-button round block type="primary" native-type="submit">
                提交
            </van-button>
        </div>
    </van-form>


</template>
<script>
    export default {
        name: 'OrderBackmoney',
        created() {
            // 接收id
            this.orderid = this.$route.query.orderid ? this.$route.query.orderid : 0
            this.price()
        },
        data() {
            return {
                orderid: 0,
                LoginUser: this.$cookies.get('LoginUser'),
                checked: true,
                text: '',
                total:0,

                // 表单验证
                rules:{
                    total:[{
                        required:true,
                        message:'请输入退款金额'
                    },
                    {
                    message:'输入格式有误',
                     validator(value){
                        //正则表达式
                        var reg=new RegExp("^(?:[1-9][0-9]*\.[0-9]+|0\.(?!0+$)[0-9]+|0)$")
                        return reg.test(value)
                       }
                    }
                    ]
                }
            }
        },

        methods: {
            back() {
                this.$router.go(-1)
            },

            // 获取价格
            async price(orderid) {

                var data = {
                    orderid: this.orderid,
                }
                var result=await this.$api.OrderPrice(data);
                // console.log(result);
                // return false
                this.total=result.data;
                // console.log(this.total);
                
            },
            // 提交表单
            async onSubmit(form) {
                // 组转数据
                var data = {
                    userid: this.LoginUser['id'],
                    orderid: this.orderid,
                    type: form.type,
                    expid: form.expid,
                    expcode: form.expcode
                }

                var result = await this.$api.OrderBackall(data);
                // console.log(result);
                // return false
                if (result.code == 0) {
                    this.$notify({
                        message: result.msg,
                        duration: 1000,
                    })
                } else {
                    this.$notify({
                        type: 'success',
                        message: result.msg,
                        duration: 1000,
                    })
                    this.$router.push({
                        path: '/user/order/index'
                    })
                }

            }
        },
    }
</script>
<style>
    .radio {
        margin-right: 1rem !important;
    }
</style>